package com.bangda.crm.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.bangda.crm.domain.CrmStockMove;
import com.bangda.crm.domain.vo.CrmStockMoveVo;
import com.bangda.crm.service.ICrmStockMoveService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.bangda.crm.domain.CrmStock;
import com.bangda.crm.service.ICrmStockService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 库存Controller
 * 
 * @author fanboshi
 * @date 2023-10-24
 */
@RestController
@RequestMapping("/crm/stock")
public class CrmStockController extends BaseController
{
    @Autowired
    private ICrmStockService crmStockService;
    @Autowired
    private ICrmStockMoveService iCrmStockMoveService;


    @PreAuthorize("@ss.hasPermi('crm:stock:edit')")
    @Log(title = "加库存", businessType = BusinessType.UPDATE)
    @PutMapping("addstock")
    public AjaxResult addstock(@RequestBody CrmStockMoveVo crmStockMoveVo)
    {
        CrmStock crmStock = new CrmStock();
        CrmStock.copyProperties(crmStockMoveVo,crmStock);
        CrmStock crmStock1 = crmStockService.selectCrmStockByStockId(crmStock.getStockId());
        BigDecimal add = crmStock1.getStockSum().add(crmStock.getStockSum());
        crmStock.setStockSum(add);

        CrmStockMove crmStockMove = new CrmStockMove();
        crmStockMove.setTradingClient(crmStockMoveVo.getTradingClient());
        crmStockMove.setStockId(crmStock.getStockId());
        crmStockMove.setQuantity(crmStockMoveVo.getStockSum());
        crmStockMove.setCreateTime(DateUtils.getNowDate());
        crmStockMove.setTradeType("进货");
        crmStockMove.setUnit("kg");
        crmStockMove.setStockName(crmStockMoveVo.getStockName());
        crmStockMove.setImg(crmStockMoveVo.getImg());
        crmStockMove.setCreationBy(getUsername());
        crmStockMove.setRemark(crmStockMoveVo.getRemark());
        crmStockMove.setMoney(crmStockMoveVo.getMoney());
        crmStockMove.setSumMoney(crmStockMoveVo.getMoney().multiply(crmStockMoveVo.getStockSum()));
        iCrmStockMoveService.insertCrmStockMove(crmStockMove);

        return toAjax(crmStockService.updateCrmStock(crmStock));
    }
    @PreAuthorize("@ss.hasPermi('crm:stock:edit')")
    @Log(title = "减库存", businessType = BusinessType.UPDATE)
    @PutMapping("reducestock")
    public AjaxResult reduceStock(@RequestBody CrmStockMoveVo crmStockMoveVo)
    {
        CrmStock crmStock = new CrmStock();
        CrmStock.copyProperties(crmStockMoveVo,crmStock);
        CrmStock crmStock1 = crmStockService.selectCrmStockByStockId(crmStock.getStockId());
        BigDecimal subtract = crmStock1.getStockSum().subtract(crmStock.getStockSum());
        crmStock.setStockSum(subtract);

        CrmStockMove crmStockMove = new CrmStockMove();
        crmStockMove.setTradingClient(crmStockMoveVo.getTradingClient());
        crmStockMove.setStockId(crmStock.getStockId());
        crmStockMove.setQuantity(crmStockMoveVo.getStockSum());
        crmStockMove.setCreateTime(DateUtils.getNowDate());
        crmStockMove.setTradeType("出货");
        crmStockMove.setUnit("kg");
        crmStockMove.setStockName(crmStockMoveVo.getStockName());
        crmStockMove.setImg(crmStockMoveVo.getImg());
        crmStockMove.setCreationBy(getUsername());
        crmStockMove.setRemark(crmStockMoveVo.getRemark());
        crmStockMove.setMoney(crmStockMoveVo.getMoney());
        crmStockMove.setSumMoney(crmStockMoveVo.getMoney().multiply(crmStockMoveVo.getStockSum()));
        iCrmStockMoveService.insertCrmStockMove(crmStockMove);
        return toAjax(crmStockService.updateCrmStock(crmStock));
    }
    /**
     * 查询库存列表
     */
    @PreAuthorize("@ss.hasPermi('crm:stock:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmStock crmStock)
    {
        startPage();
        List<CrmStock> list = crmStockService.selectCrmStockList(crmStock);
        return getDataTable(list);
    }

    /**
     * 导出库存列表
     */
    @PreAuthorize("@ss.hasPermi('crm:stock:export')")
    @Log(title = "库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmStock crmStock)
    {
        List<CrmStock> list = crmStockService.selectCrmStockList(crmStock);
        ExcelUtil<CrmStock> util = new ExcelUtil<CrmStock>(CrmStock.class);
        util.exportExcel(response, list, "库存数据");
    }

    /**
     * 获取库存详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:stock:query')")
    @GetMapping(value = "/{stockId}")
    public AjaxResult getInfo(@PathVariable("stockId") Long stockId)
    {
        return success(crmStockService.selectCrmStockByStockId(stockId));
    }

    /**
     * 新增库存
     */
    @PreAuthorize("@ss.hasPermi('crm:stock:add')")
    @Log(title = "库存", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmStockMoveVo crmStockMoveVo)
    {
        CrmStock crmStock1 = new CrmStock();
        crmStock1.setStockName(crmStockMoveVo.getStockName());
        if (crmStockService.selectCrmStockList(crmStock1).size()>0){
            return AjaxResult.error("该库存已存在");
        }

        CrmStock crmStock = new CrmStock();
        CrmStock.copyProperties(crmStockMoveVo,crmStock);
        int i = crmStockService.insertCrmStock(crmStock);

        List<CrmStock> crmStocks = crmStockService.selectCrmStockList(crmStock1);
        CrmStockMove crmStockMove = new CrmStockMove();
        crmStockMove.setTradingClient(crmStockMoveVo.getTradingClient());
        crmStockMove.setStockId(crmStocks.get(0).getStockId());
        crmStockMove.setQuantity(crmStockMoveVo.getStockSum());
        crmStockMove.setCreateTime(DateUtils.getNowDate());
        crmStockMove.setTradeType("进货");
        crmStockMove.setUnit("kg");
        crmStockMove.setStockName(crmStockMoveVo.getStockName());
        crmStockMove.setImg(crmStockMoveVo.getImg());
        crmStockMove.setCreationBy(getUsername());
        crmStockMove.setRemark(crmStockMoveVo.getRemark());
        crmStockMove.setMoney(crmStockMoveVo.getMoney());
        crmStockMove.setSumMoney(crmStockMoveVo.getMoney().multiply(crmStockMoveVo.getStockSum()));
        iCrmStockMoveService.insertCrmStockMove(crmStockMove);
        return toAjax(i);
    }

    /**
     * 修改库存
     */
    @PreAuthorize("@ss.hasPermi('crm:stock:edit')")
    @Log(title = "库存", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmStock crmStock)
    {
        return toAjax(crmStockService.updateCrmStock(crmStock));
    }

    /**
     * 删除库存
     */
    @PreAuthorize("@ss.hasPermi('crm:stock:remove')")
    @Log(title = "库存", businessType = BusinessType.DELETE)
	@DeleteMapping("/{stockIds}")
    public AjaxResult remove(@PathVariable Long[] stockIds)
    {

        return toAjax(crmStockService.deleteCrmStockByStockIds(stockIds));
    }
}
